package com.zheng.business.web.controller;

import com.zheng.business.common.constant.BusinessConstant;
import com.zheng.business.dao.use.model.StatisticsModel;
import com.zheng.business.rpc.api.BusinessApiService;
import com.zheng.common.base.BaseController;
import com.zheng.common.constant.Constant;
import com.zheng.common.util.DateUtil;
import com.zheng.common.util.JsonUtil;
import com.zheng.common.util.StringUtil;
import com.zheng.upms.dao.model.UpmsAgent;
import com.zheng.upms.dao.model.UpmsAgentExample;
import com.zheng.upms.dao.model.UpmsDict;
import com.zheng.upms.rpc.api.UpmsApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author lvg EMIAL：<a href="mailto:weiguo.liang@gh-ca.com">weiguo.liang@gh-ca.com</a> <br>
 * @version 1.0 <br>
 * @Company: GH-CA<br>
 * @ClassName: HomeController <br>
 * @Description: 首页 <br>
 * @date 2019/3/6 11:54<br>
 **/
@Controller
@Api(value = "首页", description = "首页")
@RequestMapping("/manage/home")
public class HomeController extends BaseController {

    @Autowired
    private UpmsApiService upmsApiService;

    @Autowired
    private BusinessApiService businessApiService;

    @ApiOperation(value = "首页")
    @RequiresPermissions("business:home:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index(ModelMap modelMap)
    {

        Integer agentId = upmsApiService.selectUserAgentIdByUsername(getLoginUserName());

        UpmsAgent agent = upmsApiService.selectUpmsAgentByPrimaryKey(agentId);

        UpmsAgentExample example = new UpmsAgentExample();
        UpmsAgentExample.Criteria criteria = example.createCriteria();
        criteria.andFlagEqualTo(Constant.CONST_FLAG_NORMAL);
        if(1 != agentId.intValue()){//不是总代理
            criteria.andPidEqualTo(agentId);
            example.or(example.createCriteria().andIdEqualTo(agentId));
        }//
        List<UpmsAgent> upmsAgents = upmsApiService.selectUpmsAgentByExample(example);
        modelMap.put("upmsAgents", JsonUtil.object2Json(upmsAgents));
        modelMap.put("agent", agent);
        return "/manage/home/index.jsp";
    }

    @ApiOperation(value = "销售趋势图")
    @RequiresPermissions("business:home:sales")
    @ResponseBody
    @RequestMapping(value = "/sales", method = {RequestMethod.GET})
    public Object sales(
            @RequestParam(required = true, defaultValue = "", value = "startTime") String startTime,
            @RequestParam(required = true, defaultValue = "", value = "endTime") String endTime,
            @RequestParam(required = true, defaultValue = "", value = "agentId") Integer agentId) {

        StatisticsModel modelExample = new StatisticsModel();
        //modelExample.setAgentId(agentId);
        if(agentId!=null && agentId!=Constant.CONST_SUPER_AGENTID){
            modelExample.setAgentId(agentId);
        }
        modelExample.setStartTime(DateUtil.parseDate(startTime,DateUtil.FORMAT_YYYY_MM_DD));
        modelExample.setEndTime(DateUtil.parseDate(endTime,DateUtil.FORMAT_YYYY_MM_DD));
        List<StatisticsModel> trenList = businessApiService.getOrderTrendByAgentId(modelExample);

        List<UpmsDict> dictList = upmsApiService.selectUpmsDictByCode(BusinessConstant.UM_CONSUMPTION_ORDER_STATUS);

        List<String> categoryList = DateUtil.getDateStrList(DateUtil.parseDate(startTime,DateUtil.FORMAT_YYYY_MM_DD),DateUtil.parseDate(endTime,DateUtil.FORMAT_YYYY_MM_DD));
        List<String> legendList = new ArrayList<String>();
        List<Object> serieList = new ArrayList<>();

        for(UpmsDict dict : dictList){
            legendList.add(dict.getDicText());
            Map<String,Object> serie = new HashMap<>();
            serie.put("name",dict.getDicText());
            List<Double> valueList = new ArrayList<>();
            for(String category : categoryList){
                double value = 0d;
                for(StatisticsModel model : trenList){
                    String status = StringUtil.getString(model.getStatus());
                   if(category.equals(model.getName()) && dict.getDicValue().equals(status)){
                       value = model.getValue();
                   }
                }
                valueList.add(value);
            }
            serie.put("data",valueList);
            serieList.add(serie);
        }

        Map<String,Object> dataMap = new HashMap<String,Object>();
        dataMap.put("legend",legendList);
        dataMap.put("series",serieList);
        dataMap.put("category",categoryList);
        return dataMap;
    }

    @ApiOperation(value = "利润总额")
    @RequiresPermissions("business:home:profit")
    @ResponseBody
    @RequestMapping(value = "/profit", method = {RequestMethod.GET})
    public Object profit(
            @RequestParam(required = true, defaultValue = "", value = "startTime") String startTime,
                         @RequestParam(required = true, defaultValue = "", value = "endTime") String endTime,
                         @RequestParam(required = true, defaultValue = "", value = "agentId") Integer agentId) {
        StatisticsModel modelExample = new StatisticsModel();
        //modelExample.setAgentId(agentId);
        if(agentId!=null && agentId!=Constant.CONST_SUPER_AGENTID){
            modelExample.setAgentId(agentId);
        }
        modelExample.setStartTime(DateUtil.parseDate(startTime,DateUtil.FORMAT_YYYY_MM_DD));
        modelExample.setEndTime(DateUtil.parseDate(endTime,DateUtil.FORMAT_YYYY_MM_DD));
        Double d = businessApiService.getProfitByAgentId(modelExample);
        if(d == null){
            d = 0d;
        }
        return d;
    }

    @ApiOperation(value = "累计订单")
    @RequiresPermissions("business:home:order")
    @ResponseBody
    @RequestMapping(value = "/orderSum", method = {RequestMethod.GET})
    public Object orderSum(@RequestParam(required = true, defaultValue = "", value = "startTime") String startTime,
                           @RequestParam(required = true, defaultValue = "", value = "endTime") String endTime,
                           @RequestParam(required = true, defaultValue = "", value = "agentId") Integer agentId) {
        StatisticsModel modelExample = new StatisticsModel();
        //modelExample.setAgentId(agentId);
        if(agentId!=null && agentId!=Constant.CONST_SUPER_AGENTID){
            modelExample.setAgentId(agentId);
        }
        modelExample.setStartTime(DateUtil.parseDate(startTime,DateUtil.FORMAT_YYYY_MM_DD));
        modelExample.setEndTime(DateUtil.parseDate(endTime,DateUtil.FORMAT_YYYY_MM_DD));
        Long d = businessApiService.getOrderCountByAgentId(modelExample);
        if(d == null) d=0l;
        return d;
    }

    @ApiOperation(value = "粉丝总数")
    @RequiresPermissions("business:home:fans")
    @ResponseBody
    @RequestMapping(value = "/fans", method = {RequestMethod.GET})
    public Object fans(@RequestParam(required = true, defaultValue = "", value = "agentId") Integer agentId) {
        StatisticsModel modelExample = new StatisticsModel();
        //modelExample.setAgentId(agentId);
        if(agentId!=null && agentId!=Constant.CONST_SUPER_AGENTID){
            modelExample.setAgentId(agentId);
        }
        Long d = businessApiService.getFansByAgentId(modelExample);
        if(d == null) d=0l;
        return d;
    }


    @ApiOperation(value = "预付金总额")
    @RequiresPermissions("business:home:deposit")
    @ResponseBody
    @RequestMapping(value = "/deposit", method = {RequestMethod.GET})
    public Object deposit(@RequestParam(required = true, defaultValue = "", value = "agentId") Integer agentId) {
        StatisticsModel modelExample = new StatisticsModel();
        //modelExample.setAgentId(agentId);
        if(agentId!=null && agentId!=Constant.CONST_SUPER_AGENTID){
            modelExample.setAgentId(agentId);
        }
        Double d = businessApiService.getDepositByAgentId(modelExample);
        if(d == null) d=0d;
        return d;
    }

    @ApiOperation(value = "充值余额")
    @RequiresPermissions("business:home:balance")
    @ResponseBody
    @RequestMapping(value = "/balance", method = {RequestMethod.GET})
    public Object balance(@RequestParam(required = true, defaultValue = "", value = "agentId") Integer agentId) {
        StatisticsModel modelExample = new StatisticsModel();
        //modelExample.setAgentId(agentId);
        if(agentId!=null && agentId!=Constant.CONST_SUPER_AGENTID){
            modelExample.setAgentId(agentId);
        }
        Double d = businessApiService.getBalanceByAgentId(modelExample);
        if(d == null) d=0d;
        return d;
    }

    @ApiOperation(value = "时长卡总额")
    @RequiresPermissions("business:home:timecard")
    @ResponseBody
    @RequestMapping(value = "/timecard", method = {RequestMethod.GET})
    public Object timecard(@RequestParam(required = true, defaultValue = "", value = "agentId") Integer agentId) {
        StatisticsModel modelExample = new StatisticsModel();
        //modelExample.setAgentId(agentId);
        if(agentId!=null && agentId!=Constant.CONST_SUPER_AGENTID){
            modelExample.setAgentId(agentId);
        }
        Double d = businessApiService.getTimecardByAgentId(modelExample);
        if(d == null) d = 0d;
        return d;
    }
    @ApiOperation(value = "雨伞情况统计")
    @RequiresPermissions("business:home:umbrella")
    @ResponseBody
    @RequestMapping(value = "/umbrellaStatistics", method = {RequestMethod.GET})
    public Object umbrellaStatistics(@RequestParam(required = true, defaultValue = "", value = "agentId") Integer agentId) {
        StatisticsModel modelExample = new StatisticsModel();
        //modelExample.setAgentId(agentId);
        if(agentId!=null && agentId!=Constant.CONST_SUPER_AGENTID){
            modelExample.setAgentId(agentId);
        }
        List<StatisticsModel> umbrellaStatusList = businessApiService.getUmbrellaStatisticsByAgentId(modelExample);

        List<String> legendList = new ArrayList<>();
        List<Object> serieList = new ArrayList<>();

        for(StatisticsModel model : umbrellaStatusList){
            legendList.add(model.getName());

            Map<String,Object> data = new HashMap<>();
            data.put("name",model.getName());
            data.put("value",model.getValue());
            serieList.add(data);
        }
        Map<String,Object> dataMap = new HashMap<String,Object>();
        dataMap.put("legend",legendList);
        dataMap.put("series",serieList);
        dataMap.put("name","雨伞情况统计");
        return dataMap;
    }

    @ApiOperation(value = "设备情况统计")
    @RequiresPermissions("business:home:machine")
    @ResponseBody
    @RequestMapping(value = "/machineStatistics", method = {RequestMethod.GET})
    public Object machineStatistics(@RequestParam(required = true, defaultValue = "", value = "agentId") Integer agentId) {
        StatisticsModel modelExample = new StatisticsModel();
        //modelExample.setAgentId(agentId);
        if(agentId!=null && agentId!=Constant.CONST_SUPER_AGENTID){
            modelExample.setAgentId(agentId);
        }
        List<StatisticsModel> machineStatusList = businessApiService.getMachineStatisticsByAgentId(modelExample);

//        List<UpmsDict> dictList = upmsApiService.selectUpmsDictByCode(BusinessConstant.UM_MACHINE_STATUS);
        List<String> legendList = new ArrayList<>();
        List<Object> serieList = new ArrayList<>();

        for(StatisticsModel model : machineStatusList){
            legendList.add(model.getName());

            Map<String,Object> data = new HashMap<>();
            data.put("name",model.getName());
            data.put("value",model.getValue());
            serieList.add(data);
        }
        Map<String,Object> dataMap = new HashMap<String,Object>();
        dataMap.put("legend",legendList);
        dataMap.put("series",serieList);
        dataMap.put("name","设备情况统计");
        return dataMap;
    }
}
